package com.wakey.rtcs.web.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.wakey.rtcs.web.mapper.DocInfoMapper;
import com.wakey.rtcs.web.model.Attach;
import com.wakey.rtcs.web.model.DocInfo;
import com.wakey.rtcs.web.service.DocService;

@Service
public class DocServiceImpl implements DocService{
	@Autowired
	DocInfoMapper docInfoMapper;
	
	@Override
	public List<DocInfo> queryAllDocInfo(DocInfo docInfo) {
		return docInfoMapper.queryAllDocInfo(docInfo);
	}
	
	@Override
	public int saveDocInfo(DocInfo info) {
		return docInfoMapper.save(info);
	}

	@Override
	public int deleteDocInfo(String[] ids) {
		return docInfoMapper.deleteDocInfo(ids);
	}
	
	@Override
	public Attach uploadFile(HttpServletRequest request) throws IllegalStateException, IOException {

	        Attach attach = new Attach();

	        Map<String, String> message = new HashMap<String, String>();
	        message.put("code", "200");// 初始化一个成功的代码
	        message.put("id", "");
	        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
	        
	        request.setCharacterEncoding("utf-8");

	        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
	        // 设置存放路径

	        String fileName = null;
	        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
	            // 上传文件名
	            MultipartFile mf = entity.getValue();
	            fileName = mf.getOriginalFilename();
	            //String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
	            if (mf.getSize() > 209715200) {
	                attach.setError("附件大小不能超过200M");
	                attach.setFileName(fileName);
	                return attach;
	            }
                // 创建文件夹
                String absPath = request.getRealPath("/") + "/upload";
                File file = new File(absPath);
                if (!file.exists() || !file.isDirectory()) {
                    file.mkdirs();
                }
                File uploadFile = new File(absPath + "/" + fileName);
                mf.transferTo(uploadFile);
                attach.setFileName(fileName);
	        }
	        return attach;
	   
	}


    @Override
    public boolean deleteUploadFile(HttpServletRequest request, String fileName) {
        if (StringUtils.isNotEmpty(fileName)) {
            String[] names = fileName.split(",");
            for (String name : names) {
                this.delFile(request.getRealPath("/") + "/upload/" + name);
            }
        }
        return false;
    }
    

    public static boolean delFile(String fileName) {
        File file = new File(fileName);
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

}
